package test0;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-03-28
 * Time: 16:53
 */

//https://www.nowcoder.com/questionTerminal/1277c681251b4372bdef344468e4f26e
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.StrToInt("123");
    }
    public int StrToInt(String str) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        int sl = str.length();
        int ans = 0;
        int flag = 0;
        if (Character.isDigit(str.charAt(0))) {
            ans += (int) Math.pow(10, sl - 1) * (str.charAt(0) - 48);
            flag = 1;
        } else {
            if (str.charAt(0) == '-') {
                flag = -1;
            } else {
                flag = 1;
            }
        }
        for (int i = 1; i < sl; i++) {
            char temp = str.charAt(i);
            if (temp < '0' || temp > '9') {
                return 0;
            }
            ans += (int) Math.pow(10, sl - i - 1) * (temp - 48);
        }
        return ans * flag;
    }
}


